/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.ibpPanleMidware.main;

import com.seari.commonutils.redis.JedisUtilProxy;
import com.seari.ibpPanleMidware.bean.StationConfig;
import com.seari.ibpPanleMidware.service.StationConfigService;
import com.seari.ibpPanleMidware.utils.SpringContainer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Rainbow
 */
@Component
@Order(value = 1)
public class StartUpClass implements ApplicationRunner
{
    protected Logger logger = LoggerFactory.getLogger(StartUpClass.class);
    public static List<StationConfig> stationConfigs;
    public static List<StationConfig> stationFanConfigs;
    //外层MAP key为group,内层MAP key为寄存器地址，VALUE为实际值
    public static Map<String,Map<Integer,Integer>> stationFanMap = new HashMap<>();
    public static Map<String,String[]> groupIpMap = new HashMap<>();
    private Set<String> stationDataGroup = new HashSet<>();
    
    @Autowired
    private StationConfigService stationConfigService;
    
    @Override
    public void run(ApplicationArguments args) throws Exception
    {
        System.out.println("start up class loading...");
        JedisUtilProxy.initJedisPoolUtil("127.0.0.1", 6379);
        init();
        //初始化ibpCommandProcessor
        IbpCommandProcessor ibpCommandProcessor = SpringContainer.getBean(IbpCommandProcessor.class);
        ibpCommandProcessor.stationConfigs = stationConfigs;
        new Thread(ibpCommandProcessor).start();
        //初始化ibpDataWorker
        for (String string : stationDataGroup)
        {
            IbpDataWorker ibpDataWorker = SpringContainer.getBean(IbpDataWorker.class);
            ibpDataWorker.init(string, stationConfigs);
            new Thread(ibpDataWorker).start();
        }
        
        //初始化ibpFanWorker
        IbpFanWorker ibpFanWorker = SpringContainer.getBean(IbpFanWorker.class);
        ibpFanWorker.init();
        new Thread(ibpFanWorker).start();
    }
    
    private void init()
    {
        Jedis jedis = null;
        try
        {
            stationConfigs = stationConfigService.getStationConfig();
            stationFanConfigs = stationConfigService.getStationFanConfig();
            jedis = JedisUtilProxy.getJedis();
            for (StationConfig stationConfig : stationConfigs)
            {
                if(!stationDataGroup.contains(stationConfig.getDataGroup()) && StringUtils.isNotBlank(stationConfig.getDataGroup()))
                {
                    stationDataGroup.add(stationConfig.getDataGroup());
                }
                //初始化REDIS指令点
                jedis.hset(stationConfig.getCommandTableName(), stationConfig.getStationId()+"", 0 + "");
            }
            
            //初始化ibpFan
            Map<String,String> groupIpResult = stationConfigService.getDataGroupIPMap();
            for (Map.Entry<String, String> entry : groupIpResult.entrySet())
            {
                groupIpMap.put(entry.getKey(), entry.getValue().split("\\|"));
            }
            for (StationConfig stationFanConfig : stationFanConfigs)
            {
                Map<Integer,Integer> addrValueMap = stationFanMap.get(stationFanConfig.getDataGroup());
                if(CollectionUtils.isEmpty(addrValueMap))
                {
                    addrValueMap = new HashMap<>();
                }
                addrValueMap.put(stationFanConfig.getDataAddress(), 0);//以0作为初始值
                stationFanMap.put(stationFanConfig.getDataGroup(), addrValueMap);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            JedisUtilProxy.close(jedis);
        }
    }
    
}
